<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;

use app\services\BaseServices;
use app\dao\car\BaseSuggestDao;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;

class BaseSuggestServices extends BaseServices
{
    public function __construct(BaseSuggestDao $dao){
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [], bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        foreach ($list['list'] as &$item) {
            $item['add_time'] = date('Y-m-d H:i',$item['add_time']);
            $item['thumb'] = $item['thumb'] ? json_decode($item['thumb'],true) : [];
        }
        return $list;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del (int $id) {
        if(!$this->dao->get($id)) throw new AdminException('未查询到记录');
        if(!$this->dao->update($id,['is_del'=>1,'delete_time'=>time()])) throw new AdminException('删除失败');
        return '删除成功';
    }

    /**
     * 处理
     * @param int $id
     * @param int $status
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function audit(int $id,int $status) {
        if(!$this->dao->get($id)) throw new AdminException('未查找到记录');
        if(!$this->dao->update($id,['status'=>$status])) throw new AdminException('提交失败');
        return '提交成功';
    }

    /**
     * 添加建议
     * @param $data
     * @return string
     */
    public function setUserFeedback($data){
        $data['add_time'] = time();
        if($this->dao->save($data)){
            return '提交成功，感谢您的建议';
        }else{
            throw new ValidateException('提交失败');
        }
    }
}
